﻿Imports System.Data.OleDb
Public Class ProfileEditing
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If (Not IsPostBack) Then



            Dim sql As String = "SELECT * FROM UserProfile WHERE DinerId=@f9"
            Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
            conn.Open()
            Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
            cmd.Parameters.AddWithValue("@f9", User.Identity.Name)

            Dim dr = cmd.ExecuteReader()

            dr.Read()

            If Not IsDBNull(dr("FirstName")) Then tb_FirstName.Text = dr("FirstName")

            If Not IsDBNull(dr("LastName")) Then tb_LastName.Text = dr("LastName")

            If Not IsDBNull(dr("Gender")) Then tb_Gender.Text = dr("Gender")


            If Not IsDBNull(dr("Address")) Then tb_Address.Text = dr("Address")


            If Not IsDBNull(dr("Telephone")) Then tb_Telephone.Text = dr("Telephone")
            If Not IsDBNull(dr("Email")) Then tb_Email.Text = dr("Email")

            If Not IsDBNull(dr("Portrait")) Then imgprofilepicture.ImageUrl = dr("Portrait")




            If Not IsDBNull(dr("DateOfBirth")) Then

                Dim userDoB As DateTime = dr("DateOfBirth")


                ddl_Year.SelectedValue = userDoB.Year
                ddl_Month.SelectedValue = userDoB.Month
                ddl_Day.SelectedValue = userDoB.Day
            End If
        End If



    End Sub

    Protected Sub btn_Submit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_Submit.Click
        If (Not IsPostBack) Then



            Dim newFileName As String
            newFileName = Guid.NewGuid().ToString() & getSuffix(fup_portrait.FileName)
        End If

        If fup_portrait.HasFile Then
            fup_portrait.SaveAs(Server.MapPath("pictures") & "/" & fup_portrait.FileName)
        End If



        Dim userDoB As DateTime = New DateTime(CInt(ddl_Year.SelectedValue), CInt(ddl_Month.SelectedValue), CInt(ddl_Day.SelectedValue))

        Dim sql As String = "UPDATE UserProfile SET FirstName=@f1, LastName=@f2, Portrait=@f3, gender=@f4, Address=@f5, Telephone=@f6, Email=@f7, DateOfBirth=@f8  WHERE DinerId=@f9"
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
        conn.Open()
        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)

        cmd.Parameters.AddWithValue("@f1", tb_FirstName.Text)
        cmd.Parameters.AddWithValue("@f2", tb_LastName.Text)
        If fup_portrait.HasFile Then cmd.Parameters.AddWithValue("@f3", "pictures/" & fup_portrait.FileName)
        cmd.Parameters.AddWithValue("@f4", tb_Gender.Text)
        cmd.Parameters.AddWithValue("@f5", tb_Address.Text)
        cmd.Parameters.AddWithValue("@f6", tb_Telephone.Text)
        cmd.Parameters.AddWithValue("@f7", tb_Email.Text)
        cmd.Parameters.AddWithValue("@f8", userDoB.Date)
        cmd.Parameters.AddWithValue("@f9", User.Identity.Name)

        cmd.ExecuteNonQuery()
        conn.Close()
        cmd.Dispose()
        conn.Dispose()

        Response.Redirect("/ProfileEditing.aspx")

    End Sub

    Function getSuffix(ByVal fn As String)
        Dim fileParts = fn.Split(".")
        Dim suffix = fileParts(fileParts.Length - 1)
        Return "." & suffix
    End Function

    Protected Sub tb_FirstName_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles tb_FirstName.TextChanged

    End Sub
End Class